package wxdb.ui;



import javax.swing.*;

import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;

public class Register extends JDialog{
	
	private JButton registerButton = new JButton("Register");
	private JButton cancelButton = new JButton("Cancel");
	
	private JLabel userLabel = new JLabel("User :");
	private JLabel passwdLabel = new JLabel("Password :");
	
	private JTextField userInput = new JTextField(10);
	private JPasswordField passwdInput = new JPasswordField(10);

	private JPanel pane = new JPanel();
	
	Font fon = new Font("Dialog",Font.PLAIN,15);
	
	
	void buildConstraints(GridBagConstraints gbc,int gx, int gy, int gw, int gh, int wx, int wy){
		gbc.gridx = gx;			
		gbc.gridy = gy;			
		gbc.gridwidth = gw;		
		gbc.gridheight = gh;	
		gbc.weightx = wx;		
		gbc.weighty = wy;		
	}
	
	
	private void setMyPane(){
		GridBagLayout gridbag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		pane.setLayout(gridbag);
		
		buildConstraints(constraints,0,0,3,1,2,1);
		gridbag.setConstraints(userLabel, constraints);
		pane.add(userLabel);
		
		buildConstraints(constraints,3,0,3,1,2,1);
		gridbag.setConstraints(userInput, constraints);
		userInput.setFont(fon);
		pane.add(userInput);
		
		buildConstraints(constraints,0,1,3,1,2,1);
		gridbag.setConstraints(passwdLabel, constraints);
		pane.add(passwdLabel);
		
		buildConstraints(constraints,3,1,3,1,2,1);
		gridbag.setConstraints(passwdInput, constraints);
		passwdInput.setFont(fon);
		pane.add(passwdInput);
		
		buildConstraints(constraints,0,4,3,1,2,1);
		gridbag.setConstraints(registerButton, constraints);
		pane.add(registerButton);
		
		buildConstraints(constraints,3,4,3,1,2,1);
		gridbag.setConstraints(cancelButton, constraints);
		pane.add(cancelButton);
		
		setLocation(200,80);
		setContentPane(pane);
	}
	

	private void initListener(){
		ActListener actListener = new ActListener();
		registerButton.addActionListener(actListener);
		cancelButton.addActionListener(actListener);
	}
	
	class ActListener implements ActionListener{
		public void actionPerformed(ActionEvent e){
			if(e.getSource() == registerButton)
				registerRun();
			if(e.getSource() == cancelButton){
				cancelRun();
			}
		}
	}
	
	private void registerRun(){
		String usr = userInput.getText();
		if(Global.isUserExist(usr)){
			JOptionPane.showMessageDialog(null,"User Existed , please try again","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		String pwd = new String(passwdInput.getPassword());
		if(usr.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your username please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(pwd.equals("")){
			JOptionPane.showMessageDialog(null,"Inpute your passwd please","Error" ,JOptionPane.ERROR_MESSAGE);
			return;
		}

		try{
			String fileName = "config/user.txt";
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			randomFile.seek(randomFile.length());
			String data = usr+";"+pwd+";";	

			data = data +"\r\n";
			randomFile.writeBytes(data);
			randomFile.close();
			Global.initUser();
			this.setVisible(false);
		}catch(Exception e){
			System.err.println(e.getMessage());
		};
	}
	
	private void cancelRun(){
		this.dispose();
	}

	public Register(){
		
		setTitle("Register");
		setSize(500,250);
		initListener();
		setMyPane();
		setVisible(true);
		setResizable(false);
	}
}
